package gjbsh.bangong.ds.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import gjbsh.bangong.ds.model.单位流动站;
import gjbsh.bangong.ds.service.单位流动站Service;
import gjbsh.share.DeptType;
import gjbsh.support.AppGlobalConfig;
import gjbsh.support.JsonResult;
import gjbsh.support.LoginUser;
import gjbsh.support.StringHandle;

/**
 * @author yaolongjun
 * @date   2020年5月19日 下午3:31:20
 */
@RestController
@RequestMapping("dept_liudongzhan")
public class 单位流动站Controller {
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	单位流动站Service ddService;
	
	/**
	 * @author yaolongjun
	 * @description	根据单位ID查询单位流动站相关信息
	 */
	@RequestMapping("find_custom_by_deptid")
	public JsonResult<?> findCustomByDeptId(@RequestParam("deptid")String ppDeptId,
			HttpServletRequest request){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			
			List<Map<String,Object>> mmResultsetList = new ArrayList<Map<String,Object>>();
					
			// 根据单位ID查询单位流动站信息
			mmResultsetList = ddService.findCustomByDeptId(ppDeptId);
			
			return JsonResult.getSuccessResult(mmResultsetList);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("单位流动站Controller->findCustomByDeptId:error "+e);
			return JsonResult.getErrorResult("查询单位流动站相关信息失败");
		}
	}
	
	/**
	 * @author yaolongjun
	 * @description	设置流动站建站年份
	 */
	@RequestMapping("set_ldz_year")
	public JsonResult<?> setLdzDeptYear(@RequestParam("deptid")String ppDeptId,
			@RequestParam("yijixuekeid")String ppYijixuekeId,
			@RequestParam("jianzhannianfen")String ppJianzhannianfen,
			@RequestParam("beizhu")String ppBeizhu,
			HttpServletRequest request){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			if("".equals(ppYijixuekeId)) {
				return JsonResult.getErrorResult("一级学科ID不能为空");
			}
			
			// 取当前登录用户ID
			LoginUser loginUser = (LoginUser)request.getAttribute(AppGlobalConfig.REQUEST_LOGINUSER_NAME);
			if(loginUser == null) {
				return JsonResult.getErrorResult("无登录信息");
			}
			String mmCurrentDeptType = loginUser.getDeptType() == null ? "" : loginUser.getDeptType();
			if(!mmCurrentDeptType.equals(DeptType.博管会)) {
				return JsonResult.getErrorResult("无权限设置流动站建站信息");
			}
			
			ppJianzhannianfen = ppJianzhannianfen.trim();
			ppBeizhu = ppBeizhu.trim();
			
			if("".equals(ppJianzhannianfen)) {
				return JsonResult.getErrorResult("建站年份不能为空");
			}
			if(ppJianzhannianfen.length() != 4) {
				return JsonResult.getErrorResult("建站年份不合法");
			}
			if(!"".equals(ppBeizhu)) {
				if(StringHandle.getStringByteLength(ppBeizhu) > 1000) {
					return JsonResult.getErrorResult("备注不可超过1000字节");
				}
			}
			
			// 查询要设置建站年份的流动站是否存在
			List<单位流动站> mmDeptLdzList = ddService.findByDeptIdAndYijixuekeId(ppDeptId,ppYijixuekeId);
			if(mmDeptLdzList.size() != 1) {
				return JsonResult.getErrorResult("要设置建站年份的流动站数据异常");
			}
			
			单位流动站 mmDeptLdz = mmDeptLdzList.get(0);
			mmDeptLdz.set建站年份(BigDecimal.valueOf(Integer.parseInt(ppJianzhannianfen)));
			mmDeptLdz.set备注(ppBeizhu);
			
			if(!ddService.modify(mmDeptLdz)) {
				return JsonResult.getErrorResult("设置流动站建站年份失败");
			}
			
			return JsonResult.getSuccessResult("设置流动站建站年份成果");
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("单位流动站Controller->setLdzDeptYear:error "+e);
			return JsonResult.getErrorResult("设置流动站建站年份失败");
		}
	}
	
	/**
	 * @author yaolongjun
	 * @description	根据单位ID查询单位流动站
	 */
	@RequestMapping("find_dept_liudongzhan_by_deptid")
	public JsonResult<?> find_dept_liudongzhan_by_deptid(@RequestParam("deptid")String ppDeptId,
			HttpServletRequest request){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			
			// 取当前登录用户ID
			LoginUser loginUser = (LoginUser)request.getAttribute(AppGlobalConfig.REQUEST_LOGINUSER_NAME);
			if(loginUser == null) {
				return JsonResult.getErrorResult("无登录信息");
			}
			String mmCurrentDeptId = loginUser.getDeptid() == null ? "" :loginUser.getDeptid();
			if("".equals(mmCurrentDeptId)) {
				return JsonResult.getErrorResult("无登录信息2");
			}
			
			List<Map<String,Object>> mmResultsetList = new ArrayList<Map<String,Object>>();
					
			// 判断单位ID是否等于当前登录单位
			if(ppDeptId.equals(mmCurrentDeptId)) {
				// 根据单位ID查询单位流动站
				mmResultsetList = ddService.findDeptLiudongzhanByDeptId(ppDeptId,ppDeptId);
			}else {
				// 博管会
				if(mmCurrentDeptId.equals("1")) {
					// 根据单位ID查询一级学科中本单位的流动站
					mmResultsetList = ddService.findYijixuekeLiudongzhanByDeptId(ppDeptId);
				}else {
					// 根据单位ID查询单位流动站
					mmResultsetList = ddService.findDeptLiudongzhanByDeptId(mmCurrentDeptId,ppDeptId);
				}
			}
			
			return JsonResult.getSuccessResult(mmResultsetList);
		} catch (Exception e) {
			logger.error("单位流动站Controller->find_dept_liudongzhan_by_deptid: "+e.getMessage());
			return JsonResult.getErrorResult("根据单位ID查询单位流动站失败");
		}
	}
	
	/**
	 * @author yaolongjun
	 * @description	根据单位ID查询单位流动站 (选择进站单位时用)
	 */
	@RequestMapping("find_by_deptid")
	public JsonResult<?> find_by_deptid(@RequestParam("deptid")String ppDeptId){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			
			// 根据单位ID查询单位流动站
			List<Map<String,Object>> mmDeptliudongzhanList = ddService.findDeptLiudongzhanByDeptId(ppDeptId,ppDeptId);
			
			return JsonResult.getSuccessResult(mmDeptliudongzhanList);
		} catch (Exception e) {
			logger.error("单位流动站Controller->find_by_deptid: "+e.getMessage());
			return JsonResult.getErrorResult("根据单位ID查询单位流动站失败1");
		}
	}
	
	/**
	 * @author yaolongjun
	 * @description	根据单位ID查询单位流动站(去除下级单位流动站) (选择进站单位时用)
	 */
	@RequestMapping("find_except_xjldz_by_deptid")
	public JsonResult<?> find_except_xjldz_by_deptid(@RequestParam("deptid")String ppDeptId){
		try {
			if("".equals(ppDeptId)) {
				return JsonResult.getErrorResult("单位ID不能为空");
			}
			
			// 根据单位ID查询单位流动站(去除下级单位流动站) 
			List<Map<String,Object>> mmDeptliudongzhanList = ddService.findDeptLiudongzhanExceptXjLdzByDeptId(ppDeptId);
			
			return JsonResult.getSuccessResult(mmDeptliudongzhanList);
		} catch (Exception e) {
			logger.error("单位流动站Controller->find_except_xjldz_by_deptid: "+e.getMessage());
			return JsonResult.getErrorResult("根据单位ID查询去除下级单位的单位流动站失败");
		}
	}
	

}
